package com.xuyuan.io;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Map;

/**
 * <P>常用的IO
 * <P>1. InputStream
 * <P>2. OutputStream
 * <P>3. Reader
 * <P>4. Writer
 * <P>5. BufferedReader
 * <P>6. BufferedWriter
 * <P>7. BufferedInputStream
 * <P>8. BufferedOutputStream
 *
 * @author xuyuan
 */
public class IOStudy {

    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final String FILE_PATH = "C:/Software/IDEA2022/workspace/reflect/classes" + File.separator + "json.txt";
    private static final String PATH = "C:/Software/IDEA2022/workspace/reflect/classes";

    static {
        MAPPER.writerWithDefaultPrettyPrinter();
    }

    @Test
    public void test0() throws Exception {
        try (InputStream in = Files.newInputStream(Paths.get(FILE_PATH))) {
            byte[] bytes = new byte[1024];
            int len;
            while ((len = in.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }
        } catch (IOException e) {
            throw e;
        }
    }

    @Test
    public void test1() throws Exception {
        byte[] bytes = Files.readAllBytes(Paths.get(FILE_PATH));
        String str = new String(bytes);
        JsonNode jsonNode = MAPPER.readTree(str);
        Iterator<Map.Entry<String, JsonNode>> iterator = jsonNode.fields();
        while (iterator.hasNext()) {
            Map.Entry<String, JsonNode> entry = iterator.next();
            String key = entry.getKey();
            JsonNode value = entry.getValue();
            System.out.println(key + ":" + value.asText());
        }
    }

    @Test
    public void test2() throws Exception {
        int bufferSize = 1024; // 可以根据需要调整或配置
        try (InputStream in = Files.newInputStream(Paths.get(FILE_PATH));
             OutputStream out = Files.newOutputStream(Paths.get(PATH + "/test.txt"));
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(out);
             BufferedInputStream bufferedInputStream = new BufferedInputStream(in)) {
            byte[] bytes = new byte[bufferSize];
            int length;
            while ((length = bufferedInputStream.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, length));
                bufferedOutputStream.write(bytes, 0, length);
            }
            bufferedOutputStream.flush();
        } catch (IOException e) {
            throw e;
        }
    }


    @Test
    public void test3() throws Exception {
        try (BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(FILE_PATH));
             BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(PATH + "/test2.txt"))) {
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
                bufferedWriter.write(line + System.lineSeparator());
                // bufferedWriter.write(line);
                // bufferedWriter.newLine();
            }
            bufferedWriter.flush();
        } catch (IOException e) {

        }
    }

}
